//
//  CourseCenterListViewController.m
//  xpkc
//
//  Created by TopSageOSX on 14/12/19.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "CourseCenterListViewController.h"

@interface CourseCenterListViewController ()

@end

@implementation CourseCenterListViewController{
    UIView *headView;
    UIView *headBGView;
    UIScrollView *topScroll;
    UIScrollView *bottomScroll;
    UIButton *subjectBtn;
    UIButton *classBtn;
    UIButton *searchBtn;
    UISearchBar *searchBar;
    UITableView *subjectTab;
    UITableView *classTab;
    
    UIView *contentsView;
    UITableView *tableLeft;
    UITableView *tableRight;
    NSArray *dataArray;
    NSMutableArray *currentArray;
    NSMutableArray *leftArray;
    NSMutableArray *rightArray;
    //筛选的数据
    NSMutableArray *subArray;
    NSMutableArray *classArray;
    int s[2];//筛选数组
   
    NSString * aidString;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self getCourseDataWithId:self.coursesId];
    [self creatCoursesListView];
    [self creatHeadView];
    
    
    
}
#pragma mark -
#pragma mark == creat view ==
/**
 *  创建
 */

- (void)creatHeadView{
    CGFloat offsize = 0.04*SCREEN_WIDTH;
    //视图
    headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    headView.clipsToBounds = YES;
    
    headBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    headBGView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    headBGView.clipsToBounds = YES;
    [headView addSubview:headBGView];
    
    UIView *screenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    screenView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    
    subjectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    subjectBtn.frame = CGRectMake(offsize, 5, 100, 30);
    [subjectBtn setTitle:@"全部" forState:UIControlStateNormal];
    [subjectBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    subjectBtn.backgroundColor = [UIColor whiteColor];
    subjectBtn.tag = 1;
    subjectBtn.layer.cornerRadius = 4;
    [subjectBtn addTarget:self action:@selector(showScreen:) forControlEvents:UIControlEventTouchUpInside];
    [screenView addSubview:subjectBtn];
    
    subjectTab = [[UITableView alloc]initWithFrame:CGRectMake(offsize, 40, 100, 0) style:UITableViewStylePlain];
    subjectTab.tag = 100;
    subjectTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    subjectTab.dataSource = self;
    subjectTab.delegate = self;
    subjectTab.hidden = YES;
    [headBGView addSubview:subjectTab];
    
    classBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    classBtn.frame = CGRectMake(offsize*3+100, 5, 100, 30);
    [classBtn setTitle:@"全部" forState:UIControlStateNormal];
    [classBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    classBtn.backgroundColor = [UIColor whiteColor];
    classBtn.tag = 2;
    classBtn.cornerRadios = 4;
    [classBtn addTarget:self action:@selector(showScreen:) forControlEvents:UIControlEventTouchUpInside];
    [screenView addSubview:classBtn];
    
    classTab = [[UITableView alloc]initWithFrame:CGRectMake(offsize*3+100, 40, 100, 0) style:UITableViewStylePlain];
    classTab.tag = 101;
    classTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    classTab.dataSource = self;
    classTab.delegate = self;
    classTab.hidden = YES;
    [headBGView addSubview:classTab];
    
    searchBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    searchBtn.frame = CGRectMake(SCREEN_WIDTH-45, 2, 40, 36);
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn setTitle:@"取消" forState:UIControlStateSelected];
    [searchBtn addTarget:self action:@selector(showSearch) forControlEvents:UIControlEventTouchUpInside
     ];
    [searchBtn setTintColor:GreenColor];
    [screenView addSubview:searchBtn];
    
    searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    searchBar.placeholder = @"输入搜索关键字 以空格分隔";
    searchBar.backgroundColor = [UIColor clearColor];
    searchBar.delegate = self;
    searchBar.hidden = YES;
    [headView addSubview:searchBar];
    
    [headView addSubview:screenView];
    
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(headviewSwip:)];
    [swip setDirection:UISwipeGestureRecognizerDirectionUp];
    [headView addGestureRecognizer:swip];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headviewTap:)];
    [headView addGestureRecognizer:tap];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [headView addSubview:line];
    
    headView.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.4];
    [self.view addSubview:headView];
    
}
- (void)creatCoursesListView{
    contentsView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT-64-40)];
    tableLeft = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT-40-64) style:UITableViewStylePlain];
    tableRight = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT-40-64) style:UITableViewStylePlain];
    tableLeft.tag = 1;
    tableRight.tag = 2;
    tableLeft.showsVerticalScrollIndicator = NO;
    tableLeft.delegate = self;
    tableLeft.dataSource = self;
    tableRight.delegate = self;
    tableRight.dataSource = self;
    tableLeft.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableRight.separatorStyle = UITableViewCellSeparatorStyleNone;
    [contentsView addSubview:tableLeft];
    [contentsView addSubview:tableRight];
    [self.view addSubview:contentsView];
}
/**
 *  顶部赋值
 */
- (void)refreshHeaderView{
    
    s[0] = 0;
    s[1] = 0;
    [subArray insertObject:@"全部" atIndex:0];
    [classArray insertObject:@"全部" atIndex:0];
    
    CGFloat w = 0;
    //第一层
    for (int i = 0; i<subArray.count; i++) {
        
        NSString *title = [subArray objectAtIndex:i];
        NSInteger n = title.length*18;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.frame = CGRectMake(10 + w, 0, n, 40);
        w = w + n + 10;
        btn.titleLabel.font = FONT_SYS_M;
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(screenWithTop:) forControlEvents:UIControlEventTouchUpInside];
        //默认
        if (i == 0) {
            btn.selected = YES;
            btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
        }
        [topScroll addSubview:btn];
    }
    topScroll.showsHorizontalScrollIndicator = NO;
    topScroll.contentSize = CGSizeMake(w, 40);
    w = 0;
    //第二层
    for (int i = 0; i<classArray.count; i++) {
        NSString *title = [classArray objectAtIndex:i];
        NSInteger n = title.length*18;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.frame = CGRectMake(10 + w, 0, n, 40);
        w = w + n + 10;
        btn.titleLabel.font = FONT_SYS_M;
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(screenWithBottom:) forControlEvents:UIControlEventTouchUpInside];
        //默认
        if (i == 0) {
            btn.selected = YES;
            btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
        }
        [bottomScroll addSubview:btn];
    }
    bottomScroll.showsHorizontalScrollIndicator = NO;
    bottomScroll.contentSize = CGSizeMake(w, 40);
    
    
    
    CGRect frame  = subjectTab.frame;
    frame.size.height = subArray.count*40;
    subjectTab.frame = frame;
    [subjectTab reloadData];
    CGRect frame2 = classTab.frame;
    frame2.size.height = classArray.count*40;
    classTab.frame = frame2;
    [classTab reloadData];
}




#pragma mark == func ==
/**
 *  获取数据
 *
 *  @param courseId 课程id
 */
- (void)getCourseDataWithId:(int)courseId{
    
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中";
    
    dataArray = [[NSArray alloc]init];
    currentArray = [[NSMutableArray alloc]init];
    leftArray = [[NSMutableArray alloc]init];
    rightArray = [[NSMutableArray alloc]init];
    courseId = self.coursesId;
    NSString *idString = [NSString stringWithFormat:@"%d",courseId];
    NSString *module = @"courseBuy";
    NSString *interface = @"courselist";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString,@"columnid":idString};
    
    NSLog(@"%@",idString);
    
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
        //获取列表数据
        dataArray = [dic objectForKey:@"info"];
        
        
     
        for (int i = 0; i<dataArray.count; i++) {
            if (i%2==0) {
                [leftArray addObject:[dataArray objectAtIndex:i]];
            }else{
                [rightArray addObject:[dataArray objectAtIndex:i]];
            }
        }
        [tableLeft reloadData];
        [tableRight reloadData];
        //
        //获取顶部数据
        subArray = [NSMutableArray array];
        classArray = [NSMutableArray array];
        for (int i = 0; i<dataArray.count; i++) {
         
            NSDictionary *subdic =  [dataArray objectAtIndex:i];
            NSString *subjectid =   [subdic objectForKey:@"subject_id"];
            NSString *subjectname = [subdic objectForKey:@"subject_name"];
            NSString *classid =     [subdic objectForKey:@"class_id"];
            NSString *classname =   [subdic objectForKey:@"class_name"];
           

           
            
            
            BOOL hadsub = NO;
            BOOL hadclass = NO;
            for (NSString *string in subArray) {
                if ([string isEqualToString:subjectname]) {
                    
                    hadsub = YES;
                }
            }
            if (!hadsub&&[subjectname class]!=[NSNull class]) {
                
                [subArray addObject:subjectname];
            }
            for (NSString *class in classArray) {
                if ([class isEqualToString:classname]) {
                    hadclass = YES;
                }
            }
            if (!hadclass&&[classname class] != [NSNull class]) {
                [classArray addObject:classname];
            }
        }
        
        [hud hide:YES];
        [self refreshHeaderView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        logdebug(@"%@",error.localizedDescription);
        
        [hud hide:YES];
    }];
    
}
- (void)openHeader{
    CGRect frame = headView.frame;
    frame.size.height = SCREEN_HEIGHT-64;
    headView.frame = frame;
}
- (void)closeHeader{
    CGRect frame = headView.frame;
    frame.size.height = 40;
    headView.frame = frame;
}
/**
 *  显示筛选菜单
 *
 *  @param btn btn
 */
- (void)showScreen:(UIButton *)btn{
    [self openHeader];
    for (UIGestureRecognizer *ges in headView.gestureRecognizers) {
        ges.enabled = NO;
    }
    if (btn.tag == 1) {
        subjectTab.hidden = NO;
        classTab.hidden = YES;
        [UIView animateWithDuration:0.3 animations:^{
            CGFloat he = subArray.count *40+40;
            headBGView.height = he>SCREEN_HEIGHT-64-40?SCREEN_HEIGHT-64-40:he;
        } completion:^(BOOL finished) {
            //
        }];
    }else{
        subjectTab.hidden = YES;
        classTab.hidden = NO;
        [UIView animateWithDuration:0.3 animations:^{
            CGFloat he = classArray.count *40+40;
            headBGView.height = he>SCREEN_HEIGHT-64-40?SCREEN_HEIGHT-64-40:he;
        } completion:^(BOOL finished) {
            //
        }];
    }
    searchBtn.userInteractionEnabled = NO;
    //判断是否该隐藏
    if (btn.selected) {
        [self hideScreen];
    }else{
        subjectBtn.selected = NO;
        classBtn.selected = NO;
        btn.selected = YES;
    }
}
- (void)hideScreen{
    [self closeHeader];
    //保证初始
    subjectBtn.selected = NO;
    classBtn.selected = NO;
    headBGView.height = 40;
    searchBtn.userInteractionEnabled = YES;
}
/**
 *  显示搜索栏
 */
- (void)showSearch{
    subjectTab.hidden = YES;
    classTab.hidden = YES;
    subjectBtn.selected = NO;
    classBtn.selected = NO;
    subjectBtn.userInteractionEnabled = NO;
    classBtn.userInteractionEnabled = NO;
    //此时允许手势
    for (UIGestureRecognizer *ges in headView.gestureRecognizers) {
        ges.enabled = YES;
    }
    [self openHeader];
    [UIView animateWithDuration:0.3 animations:^{
        searchBar.top = 40;
        headBGView.height = 40+40;
        contentsView.top = 40 + 40;
    } completion:^(BOOL finished) {
        //
    }];
    if (searchBtn.selected) {
        [self hideSearch];
    }else{
        searchBtn.selected = YES;
        searchBar.hidden = NO;
    }
}
- (void)hideSearch{
    [self closeHeader];
    [UIView animateWithDuration:0.3 animations:^{
        searchBar.top = 0;
        headBGView.height = 40;
        contentsView.top =  40;
    } completion:^(BOOL finished) {
        //
        searchBtn.selected = NO;
        searchBar.hidden = YES;
        subjectBtn.userInteractionEnabled = YES;
        classBtn.userInteractionEnabled = YES;
    }];
    [searchBar resignFirstResponder];
}
- (void)headviewTap:(UITapGestureRecognizer *)tap{
    [searchBar resignFirstResponder];
}
- (void)headviewSwip:(UISwipeGestureRecognizer *)swip{
    if (swip.direction == UISwipeGestureRecognizerDirectionUp) {
        [self hideSearch];
    }
}


/**
 *  第一层筛选
 *
 *  @param btn 按
 */
- (void)screenWithTop:(UIButton *)btn{
    //显示第二层
    for (id view in topScroll.subviews) {
        if ([view isKindOfClass:btn.class]) {
            UIButton *btn = view;
            btn.selected = NO;
            [UIView animateWithDuration:0.2 animations:^{
                //
                btn.transform = CGAffineTransformMakeScale(1, 1);
            }];
        }
    }
    [UIView animateWithDuration:0.3 animations:^{
        //
        btn.selected = YES;
        btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
        headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
        contentsView.frame = CGRectMake(0, 80, SCREEN_WIDTH, SCREEN_HEIGHT-64-80);
        tableLeft.frame = CGRectMake(0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT-64-80);
        tableRight.frame = CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT-64-80);
    }];
    //滑动
    CGFloat duration = 0.4;
    if (topScroll.contentSize.width>SCREEN_WIDTH) {
        if (btn.left+SCREEN_WIDTH>topScroll.contentSize.width) {
            //根据距离计时
            //太短太快，不合适
            //封
            //            CGFloat duration =
            //            ABS(topScroll.contentSize.width-SCREEN_WIDTH-topScroll.contentOffset.x)/SCREEN_WIDTH*0.4;
            //            duration = duration>0.4?duration:0.4;
            [UIView animateWithDuration:duration animations:^{
                topScroll.contentOffset = CGPointMake(topScroll.contentSize.width-SCREEN_WIDTH, 0);
            }];
        }else{
            [UIView animateWithDuration:duration animations:^{
                topScroll.contentOffset = CGPointMake(btn.left, 0);
            }];
        }
    }
    
    //第一筛选条件
    s[0] = btn.tag;
    [self screenWithSubject:s[0] andClass:s[1]];
}
/**
 *  第二层筛选
 *
 *  @param btn btn
 */
- (void)screenWithBottom:(UIButton *)btn{
    for (id view in bottomScroll.subviews) {
        if ([view isKindOfClass:btn.class]) {
            UIButton *btn = view;
            btn.selected = NO;
            [UIView animateWithDuration:0.2 animations:^{
                //
                btn.transform = CGAffineTransformMakeScale(1, 1);
            }];
            
        }
    }
    [UIView animateWithDuration:0.3 animations:^{
        //
        btn.selected = YES;
        btn.transform = CGAffineTransformMakeScale(1.15, 1.15);
    }];
    //滑动
    CGFloat duration = 0.4;
    if (bottomScroll.contentSize.width>SCREEN_WIDTH) {
        if (btn.left+SCREEN_WIDTH>bottomScroll.contentSize.width) {
            [UIView animateWithDuration:duration animations:^{
                bottomScroll.contentOffset = CGPointMake(bottomScroll.contentSize.width-SCREEN_WIDTH, 0);
            }];
        }else{
            [UIView animateWithDuration:duration animations:^{
                bottomScroll.contentOffset = CGPointMake(btn.left, 0);
            }];
        }
    }
    
    s[1] = btn.tag;
    [self screenWithSubject:s[0] andClass:s[1]];
}
/**
 *  subject筛选
 *
 *  @param sub sub
 */
- (void)screenWithSubject:(int)sub{
    //先查class
    NSString *classStr = classBtn.titleLabel.text;
    for (int i = 0; i<classArray.count; i++) {
        NSString *class = [classArray objectAtIndex:i];
        if ([classStr isEqualToString:class]) {
            [self screenWithSubject:sub andClass:i];
        }
    }
}
/**
 *  class筛选
 *
 *  @param class class
 */
- (void)screebWithClass:(int)class{
    //先查sub
    NSString *subjectStr = subjectBtn.titleLabel.text;
    for (int i = 0; i<subArray.count; i++) {
        NSString *subject = [subArray objectAtIndex:i];
        if ([subjectStr isEqualToString:subject]) {
            [self screenWithSubject:i andClass:class];
        }
    }
}
/**
 *  根据条件筛选
 *
 *  @param sub   分类
 *  @param class 班次
 */
- (void)screenWithSubject:(int)sub andClass:(int)class{
    
    //条件
    NSString *substring = [subArray objectAtIndex:sub];
    NSString *classstring = [classArray objectAtIndex:class];
    logdebug(@"%@",classstring);
    //清空数据
    [currentArray removeAllObjects];
    [leftArray removeAllObjects];
    [rightArray removeAllObjects];
    //选择
    if ([substring isEqualToString:@"全部"]) {
        [currentArray addObjectsFromArray:dataArray];
    }else{
        for (int i = 0; i<dataArray.count; i++) {
            NSDictionary *dic = [dataArray objectAtIndex:i];
            if ([substring isEqualToString:[dic objectForKey:@"subject_name"]]) {
                [currentArray addObject:dic];
            }
        }
    }
    
    //继续选择
    if ([classstring isEqualToString:@"全部"]) {
    }else{
        for (int i = 0; i<currentArray.count; i++) {
            NSDictionary *dic = [currentArray objectAtIndex:i];
            if(![classstring isEqualToString:[dic objectForKey:@"class_name"]]){
                logdebug(@"%@",classstring);
                [currentArray removeObject:dic];
                i--;
            }
        }
    }
    
    //拆分
    for (int i = 0; i<currentArray.count; i++) {
        if (i%2 == 0) {
            [leftArray addObject:[currentArray objectAtIndex:i]];
        }else{
            [rightArray addObject:[currentArray objectAtIndex:i]];
        }
    }
    [tableLeft reloadData];
    [tableRight reloadData];
}

/**
 *  搜索
 *
 *  @param searchBar searchbar
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    logdebug(@"search");
    [self hideSearch];
    //执行检索
    [self searchClassWithString:searchBar.text];
}

- (void)searchClassWithString:(NSString *)string{
    [currentArray removeAllObjects];
    //FIXME:搜索匹配率，默认我50%，自行设定
    CGFloat percent = 50;//匹配率
    NSArray *sarray = [string componentsSeparatedByString:@" "];
    for (int i = 0; i<dataArray.count; i++) {
        CGFloat fixnum =0;
        NSDictionary *dic = [dataArray objectAtIndex:i];
        for (int j = 0; j<sarray.count; j++) {
            int fix = 0;
            if ([[dic objectForKey:@"subject_name"] rangeOfString:[sarray objectAtIndex:j]].location != NSNotFound) {
                fix = 1;
            }
            if ([[dic objectForKey:@"class_name"] rangeOfString:[sarray objectAtIndex:j]].location != NSNotFound) {
                fix = 1;
            }
            if ([[dic objectForKey:@"name"] rangeOfString:[sarray objectAtIndex:j]].location != NSNotFound) {
                fix = 1;
            }
            if ([[dic objectForKey:@"tid_list"] rangeOfString:[sarray objectAtIndex:j]].location != NSNotFound) {
                fix = 1;
            }
            fixnum += fix;
        }
        if (fixnum/sarray.count >= percent/100.0) {
            [currentArray addObject:dic];
        }
    }
    [leftArray removeAllObjects];
    [rightArray removeAllObjects];
    for (int i = 0; i<currentArray.count; i++) {
        if (i%2 == 0) {
            [leftArray addObject:[currentArray objectAtIndex:i]];
        }else{
            [rightArray addObject:[currentArray objectAtIndex:i]];
        }
    }
    [tableLeft reloadData];
    [tableRight reloadData];
    //重置筛选
    [subjectBtn setTitle:@"全部" forState:UIControlStateNormal];
    [classBtn setTitle:@"全部" forState:UIControlStateNormal];
}


#pragma mark == tableview delegate ==
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //一样长
    if (tableView.tag == 100) {
        return subArray.count;
    }else if(tableView.tag == 101){
        return classArray.count;
    }else{
        return leftArray.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag >= 100) {
        return 40;
    }
    return 140.0/375.0*SCREEN_WIDTH;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag < 10) {
        if (tableView.tag == 1) {
            //left
            CourseCenterListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (cell == nil) {
                cell = [[CourseCenterListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            NSDictionary *dic = [leftArray objectAtIndex:indexPath.row];
           
            [cell setValueWithDic:dic];
            return cell;
        }else{
            //right需要验证最后有没有
            CourseCenterListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (cell == nil) {
                cell = [[CourseCenterListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            if (indexPath.row<rightArray.count) {
                cell.hidden = NO;
                NSDictionary *dic = [rightArray objectAtIndex:indexPath.row];
                [cell setValueWithDic:dic];
            }else{
                logdebug(@"空着");
                cell.hidden = YES;
                //防止用户操作
            }
            return cell;
        }
    }else{
        if (tableView.tag == 100) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secell"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secell"];
            }
            cell.textLabel.text = [subArray objectAtIndex:indexPath.row];
            cell.textLabel.font = FONT_SYS_S;
            return cell;
        }else{
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secell"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secell"];
            }
            cell.textLabel.text = [classArray objectAtIndex:indexPath.row];
            cell.textLabel.font = FONT_SYS_S;
            return cell;
        }
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 100) {
        
        [subjectBtn setTitle:[subArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
        [self screenWithSubject:indexPath.row];
        [self hideScreen];
        return;
    }
    if (tableView.tag == 101) {
        
        [classBtn setTitle:[classArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
        [self screebWithClass:indexPath.row];
        [self hideScreen];
        return;
    }
    CourseCenterListCell *cell = (CourseCenterListCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.selected = YES;
    [UIView animateWithDuration:0.1 animations:^{
        cell.selected = NO;
    }];
    
    
    NSDictionary *dics = [leftArray objectAtIndex:indexPath.row];
    CourseCenterInfoViewController *CCIVC = [[CourseCenterInfoViewController alloc]init];
    CCIVC.courseId =cell.courseaid;
    CCIVC.litpic=[dics objectForKey:@"litpic"];
    CCIVC.titleString=cell.courseTitle.text;
    [self.navigationController pushViewController:CCIVC animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.tag == 1) {
        tableRight.contentOffset = tableLeft.contentOffset;
    }else{
        tableLeft.contentOffset = tableRight.contentOffset;
    }
    if (scrollView.tag<10) {
        if (headView.frame.size.height>100) {
            //改模糊度
            return;
        }
        logdebug(@"%f",scrollView.contentOffset.y);
        if (scrollView.contentOffset.y < -30) {
            logdebug(@"imgggg");
            //先取背景图
            //FIXME:模糊背景图片
            //            UIImage *bgimg = [self.view getImage];
            //            bgimg = [bgimg blurImageWithBlur:0.1];
            //            UIImageView *image = [[UIImageView alloc]initWithFrame:contentsView.frame];
            //            image.backgroundColor = [UIColor lightGrayColor];
            //            image.image = bgimg;
            //            [self.view addSubview:image];
        }
    }
    
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView.tag<10) {
        if (scrollView.contentOffset.y<-20) {
            [self showSearch];
        }
    }
}


#pragma mark -
#pragma mark == other ==


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
